<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="微信用户id" prop="wxUserId" label-width="88px">
        <el-input
          v-model="queryParams.wxUserId"
          placeholder="请输入微信用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="openid" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="会员状态" prop="memberStatus">-->
<!--        <el-select v-model="queryParams.memberStatus" placeholder="请选择会员状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.ai_member_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ai:wxuser:add']"
        >新增</el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ai:wxuser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ai:wxuser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ai:wxuser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wxuserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微信用户id" align="center" prop="wxUserId" v-if="true"/>
      <el-table-column label="openid" align="center" prop="openid" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="头像" align="center" prop="avatar" >
        <template slot-scope="scope">
          <ImagePreview :src="scope.row.avatar"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
<!--      <el-table-column label="微信号" align="center" prop="wxNo" />-->
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="AI聊天剩余次数" align="center" prop="chatNum" />
<!--      <el-table-column label="图片转文字剩余次数" align="center" prop="ocrNum" />-->
<!--      <el-table-column label="语音转文字剩余次数" align="center" prop="voiceNum" />-->
<!--      <el-table-column label="会员状态" align="center" prop="memberStatus">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.ai_member_status" :value="scope.row.memberStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="会员等级id" align="center" prop="memberGradeId"/>-->
<!--      <el-table-column label="会员等级" align="center" prop="memberGradeName"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['ai:wxuser:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['ai:wxuser:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAddNum(scope.row)"
            v-hasPermi="['ai:wxuser:changeAINum']"
          >赠送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-minus"
            @click="handleReduceNum(scope.row)"
            v-hasPermi="['ai:wxuser:changeAINum']"
          >扣减</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>-->
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
<!--        <el-form-item label="微信号" prop="wxNo">-->
<!--          <el-input v-model="form.wxNo" placeholder="请输入微信号" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 变更数量对话框 -->
    <el-dialog :title="titleNum" :visible.sync="openNum" width="500px" append-to-body>
      <el-form ref="formNum" :model="formNum" :rules="rulesNum" label-width="100px">
        <el-form-item label="微信用户id" prop="wxUserId" v-show="false">
          <el-input v-model="formNum.wxUserId" placeholder="请输入微信用户id" />
        </el-form-item>
        <el-form-item label="业务类型" prop="bizType">
          <el-select v-model="formNum.bizType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in dict.type.change_biz_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变更数量" prop="changeNum">
          <el-input v-model="formNum.changeNum" :placeholder="changeNumTip" type="number"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formNum.remark" placeholder="请输入备注" type="textarea" autosize maxlength="140"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitFormNum">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWxuser, getWxuser, delWxuser, addWxuser, updateWxuser, changeAINum } from "@/api/ai/wxuser";
import fa from "element-ui/src/locale/lang/fa";

export default {
  name: "Wxuser",
  dicts: ['wx_status', 'ai_member_status','change_biz_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信用户表格数据
      wxuserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新人时间范围
      daterangeRegisterTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: undefined,
        phone: undefined,
        wxNo: undefined,
        registerTime: undefined,
        status: undefined,
        chatNum: undefined,
        ocrNum: undefined,
        voiceNum: undefined,
        memberStatus: undefined,
        memberGradeId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wxUserId: [
          { required: true, message: "微信用户id不能为空", trigger: "blur" }
        ],
      },

      // 修改数量的标题
      titleNum: "",
      // 是否打开修改数量
      openNum: false,
      formNum: {},
      changeNumTip: '',
      // 次数表单校验
      rulesNum: {
        wxUserId: [
          { required: true, message: "请选择微信用户", trigger: "blur" }
        ],
        bizType: [
          { required: true, message: "请选择业务变更类型", trigger: "blur" }
        ],
        changeNum: [
          { required: true, message: "请填写变更数量", trigger: "blur" }
        ],
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信用户列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRegisterTime && '' != this.daterangeRegisterTime) {
        this.queryParams.params["beginRegisterTime"] = this.daterangeRegisterTime[0];
        this.queryParams.params["endRegisterTime"] = this.daterangeRegisterTime[1];
      }
      listWxuser(this.queryParams).then(response => {
        this.wxuserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openNum =false;
      this.resetNum();
    },
    // 表单重置
    reset() {
      this.form = {
        wxUserId: undefined,
        appId: undefined,
        openid: undefined,
        unionid: undefined,
        nickname: undefined,
        avatar: undefined,
        phone: undefined,
        wxNo: undefined,
        registerTime: undefined,
        status: undefined,
        chatNum: undefined,
        ocrNum: undefined,
        voiceNum: undefined,
        memberStatus: undefined,
        memberGradeId: undefined,
        delFlag: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    // 表单重置
    resetNum() {
      this.formNum = {
        wxUserId: undefined,
        bizType: undefined,
        changeNum: undefined,
        remark: undefined
      };
      this.resetForm("formNum");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRegisterTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wxUserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const wxUserId = row.wxUserId || this.ids
      getWxuser(wxUserId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改微信用户";
      });
    },
    /** 赠送次数 */
    handleAddNum(row) {
      this.resetNum();
      const wxUserId = row.wxUserId || this.ids[0]
      this.formNum.wxUserId = wxUserId;
      this.formNum.changeNumType = 1;
      this.openNum = true;
      this.titleNum = "赠送次数";
      this.changeNumTip = '请输出正整数，例如1'
    },
    /** 扣减次数 */
    handleReduceNum(row) {
      this.resetNum();
      const wxUserId = row.wxUserId || this.ids[0]
      this.formNum.wxUserId = wxUserId;
      this.formNum.changeNumType = 2;
      this.openNum = true;
      this.titleNum = "扣减次数";
      this.changeNumTip = '请输出负整数，例如-1'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.wxUserId != null) {
            updateWxuser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addWxuser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormNum() {
      this.$refs["formNum"].validate(valid => {
        if (valid) {
          if (this.formNum.changeNumType === 1) {
            if (this.formNum.changeNum < 0) {
              this.$modal.msgError("请填写正整数");
              return;
            }
          } else if (this.formNum.changeNum > 0) {
            this.$modal.msgError("请填写负整数");
            return;
          }
          this.buttonLoading = true;
          if (this.formNum.wxUserId != null) {
            changeAINum(this.formNum).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openNum = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wxUserIds = row.wxUserId || this.ids;
      this.$modal.confirm('是否确认删除微信用户编号为"' + wxUserIds + '"的数据项？').then(() => {
        this.loading = true;
        return delWxuser(wxUserIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ai/wxuser/export', {
        ...this.queryParams
      }, `wxuser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
